#!/bin/bash

# Prints out the current down network traffic in MB
get_down_traffic()
{
	local PREFIX='  '
	local RECIEVE1=0
	local RECIEVE2=0

	local IFACES=$(ip -o link show | awk -F': ' '{print $2}')
	for IFACE in $IFACES; do
		if [ $IFACE != "lo" ]; then
			#RECIEVE1=$(($(ip -s -c link show $IFACE | tail -n3 | head -n 1 | cut -d " " -f5) + $RECIEVE1))
			RECIEVE1=$(($(ip -s -c link show $IFACE | sed -n '4p' | head -n 1 | cut -d " " -f5) + $RECIEVE1))
		fi
	done

	sleep 1

	IFACES=$(ip -o link show | awk -F': ' '{print $2}')
	for IFACE in $IFACES; do
	if [ $IFACE != "lo" ]; then
		#RECIEVE2=$(($(ip -s -c link show $IFACE | tail -n3 | head -n 1 | cut -d " " -f5) + $RECIEVE2))
		RECIEVE2=$(($(ip -s -c link show $IFACE | sed -n '4p' | head -n 1 | cut -d " " -f5) + $RECIEVE2))
	fi
	done

	if [ $(expr $RECIEVE2 - $RECIEVE1 ) -gt 1000000 ]; then
		echo "$PREFIX$(expr $(expr $RECIEVE2 - $RECIEVE1 ) / 1000000)M/s" > ~/scripts/netdownspeed.txt
	else
		echo "$PREFIX$(expr $(expr $RECIEVE2 - $RECIEVE1 ) / 1000)KB/s" > ~/scripts/netdownspeed.txt
	fi
}

get_down_traffic
